package com.example.deepschool.service.Discussion;

import com.example.deepschool.Model.Pagination;
import com.example.deepschool.Model.User;
import com.example.deepschool.exception.CustomException;
import com.example.deepschool.repositories.DiscussionRepository;
import com.example.deepschool.util.PermissionsControl;
import com.example.deepschool.util.Redis;
import lombok.SneakyThrows;

import java.util.Objects;

public class DiscussionImpl implements Discussion {
	
	private final DiscussionRepository discussionRepository;
	
	public DiscussionImpl (DiscussionRepository discussionRepository) {
		this.discussionRepository = discussionRepository;
	}
	
	@SneakyThrows
	@Override
	public Integer insert (com.example.deepschool.Model.Discussion discussion,String UUID) {
		try {
			User user = User.fromJson(Redis.HGET(Redis.GET(UUID),"user"));
			if(!PermissionsControl.VERIFY_ASK_KNOWLEDGE_POINT(user.getPermissions())){
				throw new RuntimeException("没有权限评论.");
			}
			if(discussion.getToDiscussionId()!=null){
				if(!PermissionsControl.VERIFY_ANSWER_KNOWLEDGE_POINT(user.getPermissions())){
					throw new RuntimeException("没有权限回复评论.");
				}
			}
			discussion.setFromUserId(user.getId());
			return discussionRepository.insert(discussion);
		} catch (Exception e) {
			throw new CustomException(400, e.getMessage());
		}
	}
	
	@SneakyThrows
	@Override
	public Integer delete (Long id,String UUID) {
		try {
			User user = User.fromJson(Redis.HGET(Redis.GET(UUID),"user"));
			com.example.deepschool.Model.Discussion discussion = discussionRepository.selectById(id);
			if(discussion==null){
				throw new RuntimeException("评论不存在.");
			}
			if(!PermissionsControl.VERIFY_IS_ADMIN(user.getPermissions())&&!Objects.equals(discussion.getFromUserId(), user.getId())){
				throw new RuntimeException("没有权限删除评论");
			}
			return discussionRepository.delete(id);
		} catch (Exception e) {
			throw new CustomException(400, e.getMessage());
		}
	}
	
	@SneakyThrows
	@Override
	public com.example.deepschool.Model.Discussion selectById (Long id,String UUID) {
		try {
			User user = User.fromJson(Redis.HGET(Redis.GET(UUID),"user"));
			if(!PermissionsControl.VERIFY_IS_ADMIN(user.getPermissions())){
				throw new RuntimeException("没有权限查询评论");
			}
			return discussionRepository.selectById(id);
		} catch (Exception e) {
			throw new CustomException(400, e.getMessage());
		}
	}
	
	@SneakyThrows
	@Override
	public com.example.deepschool.Model.Discussion[] selectByCourseId (Long courseId, Integer page) {
		try {
			return discussionRepository.selectByCourseId(courseId, new Pagination().page(page));
		} catch (Exception e) {
			throw new CustomException(400, e.getMessage());
		}
	}
	
	@SneakyThrows
	@Override
	public com.example.deepschool.Model.Discussion[] selectByCourseIdAndToDiscussionId (Long courseId, Long toDiscussionId, Integer page) {
		try {
			return discussionRepository.selectByCourseIdAndToDiscussionId(courseId, toDiscussionId, new Pagination().page(page));
		} catch (Exception e) {
			throw new CustomException(400, e.getMessage());
		}
	}
	
	@SneakyThrows
	@Override
	public Integer selectCountByCourseId (Long courseId) {
		try {
			return discussionRepository.selectCountByCourseId(courseId);
		} catch (Exception e) {
			throw new CustomException(400, e.getMessage());
		}
	}
	
	@SneakyThrows
	@Override
	public Integer selectCountByCourseIdAndToDiscussionId (Long courseId, Long toDiscussionId) {
		try {
			return discussionRepository.selectCountByCourseIdAndToDiscussionId(courseId, toDiscussionId);
		} catch (Exception e) {
			throw new CustomException(400, e.getMessage());
		}
	}
	
}
